<?php
/**
 * The  quiz mistakescollection
 *
 * @package   gradereport_mistakescollection
 * @author Jason <1129332567@qq.com>
 * @copyright  Jason (http://47.114.124.77)
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */
require('../../../config.php');
require_once ('Page.class.php');
require_once($CFG->dirroot.'/grade/report/mistakescollection/lib.php');
$courseid=required_param('id',PARAM_INT);
$tab = optional_param('tab', 0, PARAM_INT);
$tdir = optional_param('tdir', 3, PARAM_INT);
$course = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST);
require_login($course);
$context = context_course::instance($course->id);
require_capability('moodle/grade:viewall', $context);
$url=new moodle_url('/grade/report/mistakescollection/report.php',array("id"=>$courseid));
$PAGE->set_url($url);
$PAGE->set_pagelayout('report');
$title="测验错题集";
$PAGE->navbar->add($title);
$PAGE->set_title($title);
$PAGE->set_heading($title);
$PAGE->requires->css("/grade/report/mistakescollection/styles.css");

$where="WHERE qas.state IN(:state1,:state2) AND cm.course= :courseid";
$order=($tdir==4)?"ASC":"DESC";
$sql="SELECT qa.questionid,q.name as quizname,qa.questionid ,qa.slot, qa.questionsummary,count(*) as totalcount
      FROM {question_attempts} qa 
      JOIN {question_attempt_steps} qas ON qas.questionattemptid = qa.id AND qas.sequencenumber = ( SELECT MAX(sequencenumber) FROM mdl_question_attempt_steps WHERE questionattemptid = qa.id )
      JOIN {question_usages} qu ON qu.id=qa.questionusageid
      JOIN {context} c ON c.id=qu.contextid
      JOIN {course_modules} cm ON cm.id=c.instanceid
      JOIN {quiz} q ON q.id=cm.instance "
    .$where."  GROUP BY qa.questionid ORDER BY totalcount ".$order;

$params['state1']="gradedpartial";
$params['state2']="gradedwrong";
$params['courseid']=$courseid;

$results = $DB->get_records_sql($sql, $params);
$totalcount=count($results);
$per=10;
$page=new \gradereport_mistakescollecion\Page($totalcount,$per);
$limit=explode(',',$page->limit);
$results = $DB->get_records_sql($sql, $params,$limit[0],$limit[1]);

echo $OUTPUT->header();
$tab0url = new moodle_url('/grade/report/mistakescollection/index.php',array("id"=>$courseid));;
$tab1url=new moodle_url('/grade/report/mistakescollection/quiz.php',array("id"=>$courseid));
$title="试题答错次数统计";
$tabs = array(
    new tabobject('tab0', $tab0url, "错题列表"),
    new tabobject('tab1', $tab1url->out() . '&amp;tab=1', $title),
);
echo $OUTPUT->tabtree($tabs, "tab$tab");
echo html_writer::start_tag('div', array('class' => 'no-overflow display-table',"style"=>"text-align: center;"));
$table = new gradereport_mistakescollection_report_table($course);
$table->setup();
foreach ($results as $val) {
    $detaillink="<a href=".new moodle_url('/grade/report/mistakescollection/detailview.php',array('id'=>$courseid,'questionid'=>$val->questionid))." target='_blank' >查看</a>";
    $questionsummary=shorten_text($val->slot.". ".$val->questionsummary,120);
    $row = array ($course->shortname,$val->quizname,$questionsummary,$val->totalcount,$detaillink);
    $table->add_data($row);
}
$table->finish_output();
echo html_writer::end_tag('div');
echo "</br>".$page->fpage();
echo $OUTPUT->footer();